package routes

import (
	"arc_srv4/auth"
	_ "arc_srv4/docs"
	"arc_srv4/middleware"
	"arc_srv4/srv"
	"github.com/iris-contrib/middleware/cors"
	"github.com/kataras/iris/v12"
)

const (
	maxSize  = 5 << 20 // 5MB
	FaxeSize = 1 << 20 // 2MB
)

func Register(api *iris.Application) {
	crs := cors.New(cors.Options{
		AllowedOrigins:   []string{"*"}, // allows everything, use that to change the hosts.
		AllowedMethods:   []string{"PUT", "PATCH", "GET", "POST", "OPTIONS", "DELETE"},
		AllowedHeaders:   []string{"*"},
		ExposedHeaders:   []string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"},
		AllowCredentials: true,
	})

	api.GetRelPath()
	// 文档路由
	mainDoc := api.Party("/apidoc", crs).AllowMethods(iris.MethodOptions)
	mainDoc.Get("/", func(ctx iris.Context) {
		_ = ctx.View("/apidoc/index.html")
	})

	v1 := api.Party("/api", crs).AllowMethods(iris.MethodOptions)
	{
		// api文档中间件
		v1.Use(middleware.NewYaag())
		// 获取token
		v1.Post("/getToken", srv.GetToken)

		// 上传附件 /attachment//download/smart_case
		v1.PartyFunc("/attachment", func(attach iris.Party) {
			attach.Post("/upload", iris.LimitRequestBodySize(maxSize), srv.UploadAttachment)
			attach.Get("/download/smart_case/{name: string}", srv.DownLoadAttachment)
			attach.Get("/download/smart_case/", srv.DownLoadElectron)

		})

		// 人脸数据比对
		v1.PartyFunc("/v4", func(vx iris.Party) {
			vx.Use(auth.JwtHandler().Serve, auth.CheckToken)
			// 可在每个请求中增加中间件 如 uth.JwtHandler().Serve auth.CheckToken
			vx.PartyFunc("/face", func(arc iris.Party) {
				arc.Post("/recognition", srv.CheckFace)
				arc.Post("/bind", srv.BindFace)
				arc.Post("/get", iris.LimitRequestBodySize(FaxeSize), srv.CheckFaceInfo)

			})
		})
	}
}
